import mysql.connector
#院系分布统计
def collegeDistribution():
    # 打开数据库连接
    db = mysql.connector.connect(
        host="124.222.244.117",  # 数据库主机地址
        user="zrgj9",  # 用户名
        passwd="zrgj9",  # 密码
        database="zrgj9"
    )
    # 使用cursor()方法获取操作游标
    mycursor = db.cursor()
     # SQL 查询语句
    sql = """SELECT college,
            count(stuid) 该专业人数
            from student
            where register = 1
            group by college"""
    try:
        # 执行sql语句
        mycursor.execute(sql)
        data = mycursor.fetchall()
        # 提交到数据库执行
        db.commit()
    except:
        # 如果发生错误则回滚
        db.rollback()

    # 将元组列表转换成二维列表，进行比例换算
    for i in range(len(data)):
        data[i] = list(data[i])
    data1=[]
    for i in range(len(data)):
        dict = {}
        dict["name"] = data[i][0]
        dict["count"] = data[i][1]
        data1.append(dict)
    return data1